package com.gxa.saas.portal.controller.hhy;

import com.gxa.saas.core.entity.vo.hhy.DrowVO;
import com.gxa.saas.core.entity.vo.hhy.DrowVO02;
import com.gxa.saas.core.entity.vo.hhy.MoneyVO;
import com.gxa.saas.core.utils.JwtUtil;
import com.gxa.saas.portal.service.hhyservice.FianceService;
import com.gxa.saas.portal.service.hhyservice.SelectShopIdService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ApiOperation("账户管理相关接口")
@RestController
@RequestMapping("/finance")
@Validated
public class FinanceController {

    @Autowired
    private FianceService fianceService;

    @Autowired
    private SelectShopIdService selectShopIdService;

    @ApiOperation("可提现金额待结算")
    @GetMapping("/getMoney")
    public Map getMoney(HttpServletRequest request){
        String loginId = JwtUtil.getUsername(request.getAttribute("token1").toString());
        Integer shopId =this.selectShopIdService.getShopId(loginId);

//        List<MoneyVO> list = this.fianceService.getMoney(shopId,loginId);
        BigDecimal shopMoney = this.fianceService.shopMoney(loginId);
        BigDecimal waitMoney = this.fianceService.waitMoney(shopId);
        MoneyVO moneyVO = new MoneyVO();
        moneyVO.setShopMoney(shopMoney);
        moneyVO.setWaitMoney(waitMoney);
        List<MoneyVO> list = new ArrayList<>();
        list.add(moneyVO);

        Map map = new HashMap();
        map.put("code", 200);
        map.put("msg", "当月新增用户信息");
        map.put("data",list);
        return map;
    }

    @ApiOperation("提现")
    @PostMapping("/putMoney")
    public Map putMoney(HttpServletRequest request,@RequestBody DrowVO02 drowVO02){
        String loginId = JwtUtil.getUsername(request.getAttribute("token1").toString());
        Integer shopId =this.selectShopIdService.getShopId(loginId);

        String drowMoney = drowVO02.getDrowMoney();
        BigDecimal dorwMoney01 = BigDecimal.valueOf(Long.parseLong(drowMoney));

        System.out.println(drowVO02);
        BigDecimal shopMoney = this.fianceService.getShopMoney(loginId);

        DrowVO drowVO = new DrowVO();
        drowVO.setDrowTime(drowVO02.getDrowTime());
        drowVO.setDrowMoney(dorwMoney01);
        drowVO.setShopId(shopId);

        Map map = new HashMap();
        if(shopMoney.compareTo(dorwMoney01)>-1){
            this.fianceService.updateShopMoney(shopId,dorwMoney01,loginId);
            this.fianceService.insertDrow(drowVO);
            map.put("code", 200);
            map.put("msg", "提现成功");
            return map;
        }else /*if (shopMoney.compareTo(drowMoney)<1)*/{
            map.put("code", 500);
            map.put("msg", "提现失败");
            return map;
        }
    }

    @ApiOperation("提现记录")
    @GetMapping("/getDrow")
    public Map getDrow(HttpServletRequest request){
        String loginId = JwtUtil.getUsername(request.getAttribute("token1").toString());
        Integer shopId =this.selectShopIdService.getShopId(loginId);

        List<DrowVO> list = this.fianceService.getDrowIfo(shopId);

        Map map = new HashMap();
        map.put("code", 200);
        map.put("msg", "商铺提现信息");
        map.put("data",list);
        return map;
    }
}